package com.zer0.tga.entity.livingentity;

import com.zer0.tga.art.Bitmap;
import com.zer0.tga.art.Screen;
import com.zer0.tga.entity.Entity;
import com.zer0.tga.level.Level;
import com.zer0.tga.math.Coordinate;
import com.zer0.tga.math.Direction;

public abstract class LivingEntity extends Entity{

	public enum UnitType{ FRIENDLY, ENEMY, NEUTRAL } 
	protected UnitType unitType;
	
	protected boolean invincible = false;
	
	protected double movementSpeed = 1.0;

	public int maxHealth = 10;
	public int health;
	
	public int damage;
	
	protected int livingTime = 0;
	
	protected int hurtTime;
	protected int pushback;
	
	protected int maxHurtTime = 20;
	

	protected Direction direction = Direction.SOUTH;
	
	public LivingEntity(Coordinate position, Coordinate radius, Level level) {
		super();
		setLevel(level);
		setPositionAndRadius(position, radius);
		init();
	}

	public LivingEntity() {
		// TODO Auto-generated constructor stub
		super();
		init();
	}

	/*public void setLevel(Level level) 
	{
		super.setLevel(level);	
	}*/
	
	public void tick()
	{
		// the cycle of life and death continues
		
		livingTime++;

		action();
		
		if(health <= 0)
		{	
			die();
			this.removed=true;
		}
		
	}
	public abstract void init();
	
	public abstract void action();
	
	public abstract Bitmap getArt();
	
	public abstract boolean collision(Entity entity, Direction dir);
	
	
	public void setStartHealth(int hp) {
		maxHealth = health = hp;
	}
	
	
	public void hurt(int damage, int invincibleTime)
	{
		if (invincible)
			return;

		health-=damage;
		hurtTime=invincibleTime;
		System.out.println(this.getClass().getSimpleName() + ": " + health + "/" + maxHealth);
	}
	
	public void hurt()
	{
		hurt(1,100);
	}
	
	public void hurt(int damage)
	{
		hurt(damage,100);
	}
	
	
	
	
	public void die()
	{
		
	}
	
	public boolean blocks(Entity e) {
		// TODO Auto-generated method stub
		//return true;
		if(e instanceof LivingEntity)
			return  (((LivingEntity) e).unitType == this.unitType ? false : true) && super.blocks(e) ;
		else
			return super.blocks(e);
		
	}

	

	public void render(Screen screen) {
		// TODO Auto-generated method stub
        Bitmap bmp = getArt();

        int h = bmp.height;
        int w = bmp.width;
        
        screen.draw(bmp,position.x-w/2,position.y-h/2,position.x+w/2,position.y+h/2);
        
	}
	
}
